package com.dingyh.shopwebbackground.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dingyh.shopwebbackground.entity.Banned;
import com.dingyh.shopwebbackground.entity.Business;
import com.dingyh.shopwebbackground.entity.User;
import com.dingyh.shopwebbackground.mapper.BannedMapper;
import com.dingyh.shopwebbackground.mapper.UserMapper;
import com.dingyh.shopwebbackground.service.BusinessService;
import com.dingyh.shopwebbackground.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dingyuhan
 * @since 2023-02-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    BannedMapper bannedMapper;

    @Autowired
    BusinessService businessService;

    public User selectById(String id,String role){
        //第一种方法
        LambdaQueryWrapper<User> query = new LambdaQueryWrapper<>();
        if(id !=null && !"".equals(id) ){
            query.eq(User::getId,id);
        }
        query.eq(User::getRole, role);
        return userMapper.selectOne(query);
    }

    public User getLogin(Map<String,Object> map){

        //第二种方法
        return userMapper.getLogin(map);
    }

    //   注册
    public  User registerUer(Map<String,Object> map){

        return userMapper.selectOne((Wrapper<User>) map);
    }

//    //   买家修改个人信息
//    public User changeInformation(String id,String nickname,String tel,String mail){
//        QueryWrapper<User> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("id",id);
//        return userMapper.selectOne(queryWrapper);
//    }

    public int getByTel(String tel) {
        LambdaQueryWrapper<User> query = new LambdaQueryWrapper<>();
        query.eq(User::getTel, tel);
        return userMapper.selectCount(query);

    }

    public int getByMail(String mail) {
        LambdaQueryWrapper<User> query = new LambdaQueryWrapper<>();
        query.eq(User::getMail, mail);
        return userMapper.selectCount(query);
    }

    @Override
    public User selectByTel(String tel) {
        return userMapper.selectByTel(tel);
    }

    public int getByDisabled(User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,user.getId());
        queryWrapper.eq(User::getDisable,"0");

        return userMapper.selectCount(queryWrapper);
    }


    /**
     * 获取全部买家列表
     * @param map
     * @return
     */
    public List<User> getAllUser(Map<String,Object> map){

        return userMapper.getAllUser(map);
    }

    /**
     * 买家修改个人信息
     * @param user
     * @return
     */
    public boolean updBuy(User user) {
        try{
            return updateById(user);
        }catch (Exception e){
            System.out.println(e);
            return false;
        }
    }

    /**
     * 商家修改个人信息
     * @param user
     * @return
     */
    public boolean updShop(User user, String saleLogo) {
        user.setAvatar(saleLogo);
        Business business = businessService.selectByUserId(user.getId());
        business.setAddressDetail(user.getAddressDetail());
        business.setProvince(user.getProvince());
        business.setCity(user.getCity());
        business.setArea(user.getArea());
        try{
            return updateById(user) && businessService.updateById(business);
        }catch (Exception e){
            System.out.println(e);
            return false;
        }
    }

    public boolean endBan(User user){
        try{
            long startTime = System.currentTimeMillis();
            LambdaQueryWrapper<Banned> bannedQueryWrapper = new LambdaQueryWrapper<>();
            bannedQueryWrapper.eq(Banned::getUserid,user.getId());
            bannedQueryWrapper.isNull(Banned::getTimeend);
            Banned banned = bannedMapper.selectOne(bannedQueryWrapper);
            banned.setTimeend(startTime);
            userMapper.updateById(user);
            bannedMapper.updateById(banned);
        }catch (Exception e){
            System.out.println(e);
            return false;
        }
        return true;
    }

    @Override
    public Integer getUserCount() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getRole,"1");
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer getSaleCount() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getRole,"2");
        return userMapper.selectCount(queryWrapper);
    }
}


